package com.flyfox.modules.hr.controller;

import com.flyfox.jfinal.base.BaseController;
import com.flyfox.jfinal.component.annotation.ControllerBind;
import com.flyfox.jfinal.component.db.SQLUtils;
import com.flyfox.jfinal.template.CRUDFactory;
import com.flyfox.modules.hr.model.SalaryItem;
import com.flyfox.util.DateUtils;
import com.jfinal.plugin.activerecord.Page;

@ControllerBind(controllerKey="salaryitem")
public class SalaryItemController extends BaseController {

	private static final String path = "/pages/template/page_";
	Class<SalaryItem> cls = SalaryItem.class;
	private SalaryItem dao = SalaryItem.dao;
	
	@Override
	public void render(String view) {
		// 省点事
		setAttr("crud", CRUDFactory.instance().get(cls));
		super.render(view);
	}

	public void list() {
		SalaryItem model = getModel(cls, "attr");

		SQLUtils sql = new SQLUtils(" from tb_hr_salary_item t where 1 = 1 ");

		if (model.getAttrValues().length != 0) {
			sql.whereLike("item_name", model.getStr("item_name"));
		}

		sql.append(" order by sort ");
		
		Page<SalaryItem> page = dao.paginate(getPaginator(), "select t.* ", sql.toString().toString());
		// 下拉框
		setAttr("page", page);
		setAttr("attr", model);
		render(path + "list.bee");
	}

	public void add() {
		render(path + "add.bee");
	}

	public void view() {
		SalaryItem model = dao.findById(getParaToInt());
		setAttr("model", model);
		render(path + "view.bee");
	}

	public void delete() {
		dao.deleteById(getParaToInt());
		list();
	}

	public void edit() {
		SalaryItem model = dao.findById(getParaToInt());
		setAttr("model", model);
		render(path + "edit.bee");
	}

	public void save() {
		Integer pid = getParaToInt();
		SalaryItem model = getModel(cls);
		if (pid != null && pid > 0) { // 更新
			model.update();
		} else { // 新增
			model.put("create_id", getSessionUser().getUserID());
			model.put("create_time", DateUtils.getNow());
			model.save();
		}
		renderMessage("保存成功");
	}

}